using UnityEngine;
using UnityEngine.UI;

public class UIRoot : MonoBehaviour
{
    [SerializeField]
    private Toggle UnityPhysicsMode;

    [SerializeField]
    private Toggle CustomPhysicsMode;

    [SerializeField]
    private Button StartBtn;

    [SerializeField]
    private InputField CreateRoomCnt;

    [SerializeField]
    private Text StartBtnText;

    private bool PhysicsRunning = false;

    private void Awake()
    {
        PhysicsRunning = false;
        UnityPhysicsMode.isOn = false;
        CustomPhysicsMode.isOn = false;

        CreateRoomCnt.text = "10";

        StartBtn.onClick.AddListener(OnClickStart);
    }

    private void OnClickStart()
    {
        if (!PhysicsRunning && !UnityPhysicsMode.isOn && !CustomPhysicsMode.isOn)
        {
            Debug.LogError("至少选择一个来模拟");
            return;
        }

        var roomCntTxt = CreateRoomCnt.text;
        if (string.IsNullOrEmpty(roomCntTxt) || !int.TryParse(roomCntTxt, out var roomCnt) || roomCnt <= 0)
        {
            Debug.LogError("房间数非法！！！");
            return;
        }

        if (PhysicsRunning)
        {
            StopCurrentPhysicsRunning();
        }
        else
        {
            StartCurrentPhysicsRunning(roomCnt);
        }

        PhysicsRunning = !PhysicsRunning;

        StartBtnText.text = PhysicsRunning ? "停止" : "开始";
    }

    private void StartCurrentPhysicsRunning(int cnt)
    {
        UnityPhysicsMode.enabled = false;
        CustomPhysicsMode.enabled = false;
    }

    private void StopCurrentPhysicsRunning()
    {
        UnityPhysicsMode.enabled = true;
        CustomPhysicsMode.enabled = true;
    }
}
